package core

import (
	"embed"
	"log"
	"time"

	"github.com/faiface/beep"
	"github.com/faiface/beep/mp3"
	"github.com/faiface/beep/speaker"
)

//go:embed ring.mp3
var audioFile embed.FS

// var audioFile *os.File

var speakerInited bool = false
var streamer beep.StreamSeekCloser

func LoadSpeaker() {
	//au, err := os.Open(audioFile)
	// if err != nil {
	// 	log.Println("提示音是初始化失败！")
	// 	return
	// }
	// defer au.Close()

	au, err := audioFile.Open("ring.mp3")
	if err != nil {
		log.Println("提示音不存在！")
		return
	}

	streamerRe, format, err := mp3.Decode(au)
	if err != nil {
		log.Println("提示音初始化失败！")
		return
	}
	streamer = streamerRe
	// defer streamer.Close()

	speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
	speakerInited = true
}

func Ring() {
	if speakerInited {
		streamer.Seek(0)
		speaker.Play(beep.Seq(streamer, beep.Callback(func() {
			log.Println("bee!")
		})))
	}
}
